<template>
  <section>
    <!--工具条 高级 查询位置-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.name" placeholder="店铺名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="deptQuery">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="exportData">导出</el-button>
        </el-form-item>
        <el-form-item>
          <!-- 默认name="file"
               action="http://localhost:8080/shop/importExcel":表单发送请求的方式
          -->
          <el-upload class="upload-demo"
                     action="http://localhost:8080/shop/importExcel"
                     list-type="text">
            <el-button type="warning">点击导入</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
    </el-col>

    <!-- 数据展示列表-->
    <!--列表
        :data="department":  绑定的数据
        highlight-current-row： 高亮显示
        v-loading="listLoading"：忙等框
        @selection-change="selsChange"：勾选复选框触发事件
        sortable： 排序
    -->
    <el-table :data="shops" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="店铺名称" width="120" sortable>
      </el-table-column>
      <el-table-column prop="tel" label="店铺电话" width="120" sortable>
      </el-table-column>
      <el-table-column prop="registerTime" label="创建时间" width="120" sortable>
      </el-table-column>

      <el-table-column prop="address" label="店铺地址" width="120" sortable>
      </el-table-column>
      <!--      <el-table-column prop="logo" label="店铺logo" width="120" sortable>
            </el-table-column>-->

      <el-table-column prop="logo" label="图标" min-width="100" sortable>
        <template scope="scope">
          <img :src="imgPrefix+scope.row.logo" width="50px" height="50px">
        </template>
      </el-table-column>

      <el-table-column prop="employee.username" label="店铺员工信息" width="120" sortable>
      </el-table-column>

      <el-table-column prop="state" label="状态" width="80" sortable>
        <template scope="scope">
          <span v-if="scope.row.state==1" style="color: hotpink">待审核</span>
          <span v-if="scope.row.state==2" style="color: blue">待激活</span>
          <span v-if="scope.row.state==3" style="color: green">正常使用</span>
          <span v-if="scope.row.state==4" style="color: red">驳回</span>
        </template>
        <!-- :disabled="scope.row.state!==1":当店铺状态码不为1的时候隐藏选中（点不动这个按钮）-->
      </el-table-column>
      <el-table-column label="操作" width="250">
        <template scope="scope">
          <el-button size="small" type="info"  @click="handleEdit(scope.$index, scope.row)">修改</el-button>
          <el-button size="small" type="danger"  @click="handleDel(scope.$index, scope.row)">删除</el-button>
          <el-button type="warning" :disabled="scope.row.state!==1" size="small" @click="handleAudit(scope.$index, scope.row)">店铺审核</el-button>
        </template>
      </el-table-column>
      <!--
            <el-table-column label="操作" width="150">
              <template scope="scope">
                <el-button size="small" type="info" disabled @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button type="danger" disabled size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                <el-button type="warning" :disabled="scope.row.state!==1" size="small" @click="handleAudit(scope.$index, scope.row)">店铺审核</el-button>
              </template>
            </el-table-column>-->
    </el-table>

    <!--工具条 批量删除-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          layout="prev, pager, next"
          @current-change="handleCurrentChange"
          :page-size="pageSize"
          :total="totals"
          style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="店铺名称" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="店铺电话" prop="tel">
          <el-input v-model="saveForm.tel" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="创建时间" prop="registerTime">
          <el-input v-model="saveForm.registerTime" auto-complete="off"></el-input>
        </el-form-item>

        <!--   店铺状态：1待审核，2审核通过待激活，3激活成功，4驳回-->
        <el-form-item label="店铺状态">
          <el-radio-group v-model="saveForm.state">
            <el-radio class="radio" :label="1">待审核</el-radio>
            <el-radio class="radio" :label="2">审核通过待激活</el-radio>
            <el-radio class="radio" :label="3">激活成功</el-radio>
            <el-radio class="radio" :label="4">驳回</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="店铺地址" prop="address">
          <el-input v-model="saveForm.address" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="店铺logo" prop="logo">
          <el-input v-model="saveForm.logo" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="店长信息">
          <!-- <el-input v-model="editForm.manager_id" auto-complete="off"></el-input>-->
          <!--
            v-for="item in cities":  循环
             :label:  展示的名字
             :value: 向后端传的值
          -->
          <el-select clearable v-model="saveForm.admin_id" placeholder="请选择">
            <el-option
                v-for="item in employees"
                :label="item.username"
                :value="item.id">
              <span style="float: left">{{item.username}}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{item.phone}}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>

    <!-- 审核模态框 -->
    <el-dialog title="审核店铺" :visible.sync="shopAuditVisible" :close-on-click-modal="false">
      <el-form :model="shopAuditLog" label-width="80px"  ref="shopAuditLogForm">
        <el-form-item label="备注" prop="note">
          <el-input type="textarea" v-model="shopAuditLog.note"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="shopAuditVisible = false">取消</el-button>
        <el-button type="primary" @click.native="auditPass">通过</el-button>
        <el-button type="primary" @click.native="auditReject">驳回</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>

export default {
  data() {
    return {
      //店铺审核编辑界面数据
      shopAuditLog: {
        shop_id: null,
        note:''
      },
      imgPrefix:"http://123.207.27.208",
      employees:[],
      title: "",
      filters: {
        name: ''
      },
      shops: [],
      totals: 0,
      currentPage: 1,
      pageSize: 5,
      listLoading: false,
      sels: [],//列表选中列


      shopAuditVisible: false,//审核模态框
      saveFormVisible: false,//编辑界面是否显示
      saveLoading: false,

      // 校验规则
      saveFormRules: {
        name: [
          { required: true, message: '请输入店铺名称', trigger: 'blur' }
        ],
        tel: [
          { required: true, message: '请输入店铺电话', trigger: 'blur' }
        ]
      },
      //编辑和修改界面数据
      saveForm: {
        id:null,
        name:'',
        tel:'',
        registerTime:new Date(),
        state:1,
        address:'',
        //logo:'',
        admin_id:null,
      },
    }
  },
  methods: {
    //高级查询
    deptQuery(){
      //当前页默认值为1
      this.currentPage = 1;
      //刷新页面
      this.getShops();
    },

    //页面改变事件
    handleCurrentChange(currentPage) {
      //将前台页面点击到的页码赋给当前页
      this.currentPage = currentPage;
      // 从新发送请求
      this.getShops();
    },
    //获取店铺信息
    /* 传递的参数：pageSize：每一页展示的数据
               currentPage:当前页参数
               keyWords:高级查询关键字*/
    getShops(){
      let para = {
        pageSize:this.pageSize,
        currentPage:this.currentPage,
        keyWords: this.filters.name //高级查询传递的参数
      };
      //开启忙等框
      this.listLoading = true;
      //发送求情
      this.$http.post("/shop",para).then((res)=>{
        //关闭忙等框
        this.listLoading = false;
        //拿到后端传回的数据 与后端代码ListPageL里面的参数一致
        this.shops=res.data.data;
        //数据总条数 通过后端查询总条数方法得到并传回
        this.totals=res.data.totals;
      }).catch((res)=>{
        this.res.$message({
          message:"系统繁忙，请稍后再试",
          type:"error",
        })
      })
    },

    //查询员工信息作用于编辑和修改时的选择
    getEmployees(){
      //发送求情
      this.$http.get("/employee").then((res)=>{
        this.employees = res.data;
      })
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      //拿到员工信息并回显
      this.getEmployees();
      this.title = "编辑";
      this.saveFormVisible = true;

      // 克隆一份数据，然后回显数据
      //row:一个对象，也就是一条数据信息
      this.saveForm = Object.assign({}, row);
    },

    //显示新增界面
    handleAdd: function () {
      //拿到员工信息
      this.getEmployees();
      this.title = "新增";
      this.saveFormVisible = true; // 弹出模态框
      // 清空模态框
      this.saveForm = {
        id:null,
        name:'',
        tel:'',
        registerTime:new Date(),
        state:1,
        address:'',
        logo:'',
        admin_id:null,
      };
    },

    //提交按钮
    saveSubmit(){
      //表单校验
      this.$refs.saveForm.validate((valid) => {
        //表单校验成功=>可以发送请求
        if (valid) {
          this.$confirm('确认提交吗？', '温馨提示', {}).then(res=>{
            //开启忙等框
            this.listLoading = true;
            // 克隆数据 给saveForm赋值一个空对象 避免影响双向绑定:修改时要传给后端的数据
            let para = Object.assign({}, this.saveForm);
            //发送请求
            this.$http.put("/shop",para).then((res)=>{
              //关闭忙等框
              this.listLoading = false;
              if(res.data.success){
                this.$message({
                  message:"操作成功",
                  type:"success",
                })
              }else{
                this.$message({
                  message:res.data.msg,
                  type:"error",
                })
              }
            })
            // 关闭模态框
            this.saveFormVisible = false;
            //刷新页面
            this.getShops();
          }).catch((res)=>{
            this.$message({
              message:"系统繁忙，请稍后再试",
              type:"error",
            })
          })
        }
      })
    },

    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '温馨提示', {
        type: 'warning'
      }).then((res) => {
        //开启忙等框
        this.listLoading = true;
        //发送请求
        this.$http.delete("/shop/"+row.id).then((res) => {
          //关闭忙等框
          this.listLoading = false;
          if (res.data.success) {
            this.$message({
              message: "操作成功",
              type: "success",
            })
          }else{
            this.$message({
              message: res.data.msg,
              type: "error"
            })
          }
          //刷新页面
          this.currentPage=1;
          this.getShops();
        }).catch(res => {
          this.$message({
            message: "系统繁忙，请稍后再试",
            type: "error",
          })
        })
      }).catch(res=>{

      })
    },

    //批量选中
    selsChange: function (sels) {
      this.sels = sels;
      console.debug(this.sels)
    },

    //批量删除
    batchRemove(){
      //定义变量
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '温馨提示', {
        type: 'warning'
      }).then((res)=>{
        //开启忙等框
        this.editLoading = true;
        //发送请求
        this.$http.patch("/shop",ids).then((res)=>{
          if(res.data.success){
            this.$message({
              message:"操作成功",
              type:"success",
            })
          }else{
            this.$message({
              message:res.data.msg,
              type:"error",
            })
          }
          //刷新页面
          this.currentPage = 1;
          this.getShops();
        }).catch((res)=>{
          this.$message({
            message:"系统繁忙，请稍后再试",
            type:"error",
          })
        })
      })
    },

    //点击店铺审核弹出模态框
    handleAudit: function (index, row) {
      this.shopAuditLog.shop_id = row.id;
      this.shopAuditVisible = true;

      /*this.shopAuditLog = {
          shop_id: null,
          note:'',
      };*/
    },
//审核驳回
    auditReject(){
      this.$http.post("/audit/reject",this.shopAuditLog).then(res=>{
        if(res.data.success){
          this.$message({
            message: '驳回成功',
            type: 'success'
          });
        }else{
          this.$message({
            message: '驳回失败',
            type: 'error'
          });
        }
        this.shopAuditVisible = false;
        this.getShops();
      })
    },
    //审核通过
    auditPass(){
      this.$http.post("/audit/pass",this.shopAuditLog).then(result=>{
        result = result.data;
        if(result.success){
          this.$message({
            message: '审核通过',
            type: 'success'
          });
        }else{
          this.$message({
            message: '审核失败',
            type: 'error'
          });
        }
        this.shopAuditVisible = false;
        this.getShops();
      })
    },

    //数据导出
    exportData(){

      //this.$router.push({ path: '/register' }); //该请求要经过路由
      //普通的发请求的方式，没有跨域问题，因为不是异步请求
      location.href="http://localhost:8080/shop/export";
    }
  },



  mounted() {
    this.getShops();
  }
}

</script>




<style scoped>

</style>
